package org.mogu.task;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public abstract class UrlTask {

	private Thread th;
	public abstract String setUrl();
	public abstract void handleResult(String result);
	public abstract void handleException();
	public UrlTask()
	{
		 th = new Thread()
	        {
	        	public void run()
	        	{
	        		String httpUrl = setUrl();
	        		String strResult = "";
	            	//获得的数据
	            	HttpGet httpRequest = new HttpGet(httpUrl);
	            	try
	            	{
	            		//构建一个HttpClient对象
	            		HttpClient httpclient = new DefaultHttpClient();
	            		//请求HttpClient，取得HttpResponse
	            		HttpResponse httpResponse = httpclient.execute(httpRequest);
	            		//请求成功
	            		if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
	            		{
	            			//取得返回的字符串
	            			strResult = EntityUtils.toString(httpResponse.getEntity());
	            		}
	             	}
	            	catch (Exception e) 
	    			{
	            		handleException();            		
	    			}
	            	handleResult(strResult);
	        	}
	        };
	}
	public void  start()
	{
		th.start();
	}
}
